import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'

const store = new Vuex.Store({
    state: {
        token: null,
        user: null,
        isLogin: false
    },
    getters: {
        getToken(state) {
            return state.token;
        },
        getUser(state) {
            return state.user;
        },
        getIsLogin(state) {
            return state.isLogin;
        }
    },
    mutations: {
        SetToken(state, token) {
            state.token = token;
        },
        LoginIn(state, user) {
            state.user = user;
            state.isLogin = true;
        },
        LoginOut: state => {
            state.token = null;
            state.user = null;
            state.isLogin = false;
        }
    },
    actions: {
        SetToken(context, token) {
            context.commit('SetToken', token);
        },
        LoginIn(context, user) {
            context.commit('LoginIn', user);
        },
        LoginOut(context) {
            context.commit('LoginOut');
        }
    },
    plugins: [
        createPersistedState({
            storage: window.localStorage
        })
    ]
})

export default store